﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Runtime.Serialization.Formatters.Binary;
using Domen;
using Sesija;

namespace Server
{
    public class ObradaZahtevaKlijenata
    {
        Broker b = Broker.DajInstancu();
        NetworkStream tok;
        BinaryFormatter formater;

        public ObradaZahtevaKlijenata(NetworkStream tok)
        {
            formater = new BinaryFormatter();
            this.tok = tok;
            ThreadStart ts = ObradaPodataka;
            Thread nit = new Thread(ts);
            nit.Start();
        }
        private void ObradaPodataka()
        {
            int operacija = 0;
            while (operacija != (int)Operacije.KRAJ)
            {
                TransferKlasa transfer = formater.Deserialize(tok) as TransferKlasa;
                operacija = transfer.Operacija;

                switch (operacija)
                {
                    case (int)Operacije.SACUVAJ_MESTO:
                        transfer.Signal = b.UbaciMesto(transfer.TransferObjekat as Mesto);
                        formater.Serialize(tok, transfer);
                        break;
                    case (int)Operacije.VRATI_MESTA:
                        transfer.TransferObjekat = b.VratiSvaMesta();
                        formater.Serialize(tok, transfer);
                        break;
                    case (int)Operacije.KRAJ:
                        Console.WriteLine("Klijent je diskonektovan!");
                        break;
                }
            }
        }
    }
}
